package com.liujit.bms.pc.config;

import com.liujit.common.exception.DataNotFoundException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 全局异常拦截器
 * @Author: liujun
 * @Date: 2021/7/3 3:23 下午
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理通用自定义业务异常
     */
    @ExceptionHandler(DataNotFoundException.class)
    public String handleBusinessException(DataNotFoundException e, HttpServletRequest request) {
        log.info("BusinessException start, uri:{}, exception:{}, caused by: {}", request.getRequestURI(), e.getClass(),
                e.getMessage());
        return "404";
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public String handleNoHandlerFoundException(Throwable ex) {
        log.error(ex.getMessage());
        return "404";
    }

    @ExceptionHandler(Throwable.class)
    public String handleThrowable(Model model, Throwable ex) {
        log.error(ex.getMessage());
        model.addAttribute("errInfo", ex.getMessage());
        return "500";
    }
}
